package com.zou.controller.proscenium;

import com.zou.commons.constants.Constants;
import com.zou.pojo.Article;
import com.zou.pojo.Column;
import com.zou.service.ArticleService;
import com.zou.service.ColumnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller("prosceniumIndex")
public class IndexController {
    @Autowired
    private ColumnService columnService;
    @Autowired
    private ArticleService articleService;

    /**
     * 前台首页
     *
     * @param model
     * @return
     */
    @RequestMapping({"/proscenium/index", "/index"})
    public String index(Model model) {
        // 查询所有状态启用的栏目
        List<Column> columns = columnService.querySomeColumnByColumn(new Column().setColumnState(Constants.STATE_ENABLED));
        model.addAttribute("columns", columns);
        // 根据栏目id分别查询文章封装在List集合中
        List articlesList = new ArrayList<>();
        for (Column column : columns) {
            List<Article> as = articleService.queryTitleAndIdByColumnId(column.getColumnId());
            articlesList.add(as);
        }
        model.addAttribute("articlesList", articlesList);
        // 查询五个浏览量最多的文章
        List<Article> articles = articleService.queryFivePageViewsMaxArticle();
        model.addAttribute("articles", articles);
        return "proscenium/index.html";
    }
}
